package com.tongji.showroom.view
{
	import alternativa.protocol.codec.NullMap;
	
	import com.tongji.showroom.events.ChangePropertyEvent;
	import com.tongji.showroom.events.FilterEvent;
	import com.tongji.showroom.events.SearchSceneEvent;
	import com.tongji.showroom.events.ShowRoomEvent;
	import com.tongji.showroom.model.FurnitureModel;
	import com.tongji.showroom.utils.Blending;
	import com.tongji.showroom.utils.ConstVariableInventory;
	import com.tongji.showroom.utils.DragDropManager;
	import com.tongji.showroom.utils.PopUpAdmin;
	import com.tongji.showroom.view.titleWindows.SingleFurnitureTitleWindow;
	import com.tongji.showroom.view.twoD.AddBitmap;
	import com.tongji.showroom.view.twoD.Furniture;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.core.IUIComponent;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.events.FlexMouseEvent;
	import mx.managers.DragManager;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
		
	/**
	 * 
	 * @author yanghanbing
	 * 2011/08/
	 */ 
	public class WorkCompMediator extends Mediator
	{
		[Inject]
		public var view:WorkComp;
		
		[Inject]
		public var furnitureModel:FurnitureModel;
		
		private var singleFurnitureTitleWindow:SingleFurnitureTitleWindow = new SingleFurnitureTitleWindow;
		
		[Bindable]
		private var room:Furniture;
		
		override public function onRegister():void
		{
			eventMap.mapListener(view, DragEvent.DRAG_ENTER, viewDragEnterHandler, DragEvent);
			eventMap.mapListener(view, DragEvent.DRAG_DROP, viewDragDropHandler, DragEvent);
			eventMap.mapListener(view, DragEvent.DRAG_OVER, viewDragOverHandler, DragEvent);
			addViewListener(ChangePropertyEvent.CHANGE_FURNITURE, furnitureClickHandler, ChangePropertyEvent);
			this.addContextListener(ChangePropertyEvent.ENLARGE_FURNITURE, enlargePic, ChangePropertyEvent);
			this.addContextListener(ChangePropertyEvent.SHRINK_FURNITURE, shrinkPic, ChangePropertyEvent);
			this.addContextListener(ChangePropertyEvent.DELETE_FURNITURE, deletePic, ChangePropertyEvent);	
			this.addContextListener(FilterEvent.SET_FILTER, setFilterHandler, FilterEvent);
		}
		
		private function viewDragOverHandler(event:DragEvent):void
		{
			room = event.dragSource.dataForFormat(event.dragSource.formats[0]) as Furniture;
			
			//缩放。将家具拖到舞台时其scale值不为1，因此设定explicitWidth为其所显示的宽度
			/*room.scaleX = ConstVariableInventory.RECT_WIDTH / ConstVariableInventory.PICTURE_WIDTH;
			room.scaleY = ConstVariableInventory.RECT_HEIGHT / ConstVariableInventory.PICTURE_HEIGHT;	*/	
			room.scaleX = 0.36;
			room.scaleY = 0.36;
			room.explicitWidth = room.content.width * room.scaleX;
			room.explicitHeight = room.content.height * room.scaleY;
			
			// 将家具拖到舞台时首次检查是否出界
			var upOut:Boolean = false;
			var downOut:Boolean = false;		
			var leftOut:Boolean = false;
			var rightOut:Boolean = false;
			
			//四周是否超界
			upOut = event.localY < view.y;
			downOut = (event.localY + room.explicitHeight) > ConstVariableInventory.RECT_HEIGHT;
			leftOut = event.localX < view.x;
			rightOut = (event.localX + room.explicitWidth) > ConstVariableInventory.RECT_WIDTH;
			
			if (upOut || downOut || leftOut || rightOut)
				DragManager.showFeedback(DragManager.NONE);
			else
				DragManager.showFeedback(DragManager.MOVE);
		}
		
		private function viewDragEnterHandler(event:DragEvent):void
		{
			DragManager.acceptDragDrop(IUIComponent(event.target));
		}
		
		private function viewDragDropHandler(event:DragEvent):void
		{
			room = event.dragSource.dataForFormat(event.dragSource.formats[0]) as Furniture;
			
			//缩放。将家具拖到舞台时其scale值不为1，因此设定explicitWidth为其所显示的宽度
			/*room.scaleX = ConstVariableInventory.RECT_WIDTH / ConstVariableInventory.PICTURE_WIDTH;
			room.scaleY = ConstVariableInventory.RECT_HEIGHT / ConstVariableInventory.PICTURE_HEIGHT;	*/	
			room.scaleX = 0.36;
			room.scaleY = 0.36;
			room.explicitWidth = room.content.width * room.scaleX;
			room.explicitHeight = room.content.height * room.scaleY;
					
			// 将家具拖到舞台时首次检查是否出界
			var upOut:Boolean = false;
			var downOut:Boolean = false;		
			var leftOut:Boolean = false;
			var rightOut:Boolean = false;
			
			//四周是否超界
			/*upOut = event.localY < room.explicitHeight;
			downOut = (event.localY + room.explicitHeight) > ConstVariableInventory.RECT_HEIGHT;
			leftOut = event.localX < room.explicitWidth;
			rightOut = (event.localX + room.explicitWidth) > ConstVariableInventory.RECT_WIDTH;*/
			
			if (upOut || downOut || leftOut || rightOut)
				Alert.show("不能拖到此处！");
			else
			{		
				if (furnitureModel.furniturCount == 0)
				{
					view.addChild(room);
					view.numChildren;
					/*room.x = event.localX - ConstVariableInventory.DRAG_PICTURE_WIDTH;
					room.y = event.localY - ConstVariableInventory.DRAG_PICTURE_HEIGHT;*/
					room.x = view.mouseX;
					room.y = view.mouseY;
					
					var backadd:AddBitmap = view.getChildAt(0) as AddBitmap;
					var backbitmap:BitmapData = backadd.addBitmapData;	
					room.setColor(Blending.blending(backbitmap, room.content, room.x, room.y));
					furnitureModel.furniturCount++;	
					dispatch(new SearchSceneEvent(SearchSceneEvent.DRAG_ROOM, 0, null, null, 0, 0, null,room));
				}
				else
					Alert.show("对不起，您已经添加了一个商品");	
			}
		}
				
		private function furnitureClickHandler(event:ChangePropertyEvent):void
		{
			PopUpManager.addPopUp(singleFurnitureTitleWindow, view);
			mediatorMap.createMediator(singleFurnitureTitleWindow);
			singleFurnitureTitleWindow.x = room.x + room.explicitWidth + ConstVariableInventory.COMP_X / ConstVariableInventory.X_SCALE;
			singleFurnitureTitleWindow.y = room.y + room.explicitHeight + ConstVariableInventory.COMP_Y / ConstVariableInventory.Y_SCALE;
			/*singleFurnitureTitleWindow.x = room.x + room.explicitWidth / ConstVariableInventory.X_SCALE + 39;
			singleFurnitureTitleWindow.x = room.x / ConstVariableInventory.X_SCALE + room.explicitWidth;
			singleFurnitureTitleWindow.x = (room.x + room.explicitWidth) / ConstVariableInventory.X_SCALE + 39;
			room.singleFurnitureTitleWindow.mouseY
			*/
			singleFurnitureTitleWindow.owner = event.fur;
		}
		
		public function enlargePic(event:ChangePropertyEvent):void
		{
			//右侧和下方是否超界
			var down:Boolean = false;	
			var right:Boolean = false;
			
			down = (room.y + room.explicitHeight * 1.05) > ConstVariableInventory.RECT_HEIGHT;
			right = (room.x + room.explicitWidth * 1.05) > ConstVariableInventory.RECT_WIDTH;
			
			if (down || right)
				Alert.show("超界！");
			else
			{
				room.scaleX *= 1.05;
				room.scaleY *= 1.05;
				room.explicitWidth = room.content.width * room.scaleX;
				room.explicitHeight = room.content.height * room.scaleY;

				singleFurnitureTitleWindow.x = room.x + room.explicitWidth + ConstVariableInventory.COMP_X / ConstVariableInventory.X_SCALE;
				singleFurnitureTitleWindow.y = room.y + room.explicitHeight + ConstVariableInventory.COMP_Y / ConstVariableInventory.Y_SCALE;
				
				/*singleFurnitureTitleWindow.x = room.x + room.explicitWidth;
				singleFurnitureTitleWindow.y = room.y + room.explicitHeight;*/		

			}	
			dispatch(new SearchSceneEvent(SearchSceneEvent.DRAG_ROOM, 0, null, null, 0, 0, null,room));
		}
		
		public function shrinkPic(event:ChangePropertyEvent):void
		{
			room.scaleX *= 0.95;
			room.scaleY *= 0.95;
			room.explicitWidth = room.content.width * room.scaleX * 0.95;
			room.explicitHeight = room.content.height * room.scaleY * 0.95;
			singleFurnitureTitleWindow.x = room.x + room.explicitWidth + ConstVariableInventory.COMP_X / ConstVariableInventory.X_SCALE;
			singleFurnitureTitleWindow.y = room.y + room.explicitHeight + ConstVariableInventory.COMP_Y / ConstVariableInventory.Y_SCALE;
			
			/*singleFurnitureTitleWindow.x = room.x + room.explicitWidth;
			singleFurnitureTitleWindow.y = room.y + room.explicitHeight;	
			dispatch(new SearchSceneEvent(SearchSceneEvent.DRAG_ROOM, null, null, 0, 0, null,room));
		*/}
		
		public function deletePic(event:ChangePropertyEvent):void
		{
			mediatorMap.removeMediatorByView(singleFurnitureTitleWindow);
			view.removeChild(event.fur);
			furnitureModel.furniturCount--;
		}
		
		private  function setFilterHandler(event:FilterEvent):void
		{
			room.setFilter(event.dropFilter);
		}
		
	}
}